

import 'package:xim/common/get_style_view.dart';
import 'package:xim/http/apis/user/user_api.dart';
import 'package:xim/http/apis/user/user_model.dart';

class SessionCreatePageController extends GetxStateMixinController<List<UserFriendModel>> {

  List<StatefulUser> statefulUsers = [];

  @override
  Future<List<UserFriendModel>?> dataInit() async {
    var users = await UserApi.queryFriends();
    statefulUsers = users.map((e) => StatefulUser(e, isSelected: false)).toList();
    return users;
  }

  changeUserStatus(int index, bool isSelected){
    statefulUsers[index].isSelected = isSelected;
    update();
  }

  changeUserStatusById(int id, bool isSelected){
    for(var u in statefulUsers){
      if(u.userFriendModel.id == id){
        u.isSelected = isSelected;
        update();
        break;
      }
    }
  }

  Future<UserInfoModel?> searchUser(String account) async {
     var res = await UserApi.searchByAccount(account);
     if(res.isEmpty){
       return null;
     }
     return res[0];
  }

}

class StatefulUser {

  final UserFriendModel userFriendModel;

  bool isSelected;

  StatefulUser(this.userFriendModel, {this.isSelected=false});
}